package com.wing.cabinet.controller.app;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.cabinet.model.entity.Cabinet;
import com.wing.cabinet.model.entity.CellInventoryOutAmount;
import com.wing.cabinet.model.entity.CellInventorySpace;
import com.wing.cabinet.service.CellInventoryOutAmountService;
import com.wing.common.utils.JsonResult;
import com.wing.setting.model.entity.SystemRuleInfo;
import com.wing.setting.service.SystemRuleInfoService;
import com.wing.web.utils.JwtUtils;
import com.wing.web.utils.LocaleMessage;
import com.wing.common.utils.PageResult;
import com.wing.cabinet.model.entity.CellSingleInventoryProduct;
import com.wing.cabinet.model.response.CellSingleInventoryProductVO;
import com.wing.cabinet.service.CellSingleInventoryProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.wing.cabinet.model.request.CellSingleInventoryProductForm;
import org.springframework.validation.annotation.Validated;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author
 */
@Validated
@Api(value = "App-售刀柜sku库存(某个柜子)管理", tags = "售刀柜sku库存(某个柜子)管理")
@RestController("appCellSingleInventoryProductController")
@RequestMapping("cabinet/app/cellSingleInventoryProduct")
public class CellSingleInventoryProductController {

    @Autowired
    private CellSingleInventoryProductService cellSingleInventoryProductService;
    @Autowired
    private LocaleMessage localeMessage;

    @Autowired
    private SystemRuleInfoService systemRuleInfoService;

    @Autowired
    private CellInventoryOutAmountService cellInventoryOutAmountService;

    @ApiOperation(value = "查询货道上产品库存(领用列表)", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "productCategoryId", value = "产品分类id", defaultValue = "", example = "", dataType = "Long", paramType = "query")
            ,@ApiImplicitParam(name = "productTypeId", value = "产品类型id", defaultValue = "", example = "", dataType = "Long", paramType = "query")
            ,@ApiImplicitParam(name = "skuName", value = "SKU名称", defaultValue = "", example = "", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "brandName", value = "品牌名称", defaultValue = "", example = "", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "typeName", value = "产品类型名称", defaultValue = "", example = "", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "cabCodes", value = "柜子编号", defaultValue = "", example = "", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<CellSingleInventoryProductVO> list(
            @RequestParam(name = "page",  defaultValue = "1", required = false) Integer page
            ,@RequestParam(name = "limit",  defaultValue = "10", required = false) Integer limit
            ,@RequestParam(name = "sort",  defaultValue = "create_time", required = false) String sort
            ,@RequestParam(name = "order",  defaultValue = "asc", required = false) String order
            ,Long productCategoryId
            ,Long productTypeId
            ,String cabCodes
            ,String skuName
            ,String brandName
            ,String typeName
            ,String name
            ,Long workOrderId
    ) {
        Page<CellSingleInventoryProductVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }
        params.put("productCategoryId",productCategoryId);
        params.put("productTypeId",productTypeId);
        if (StringUtils.isNotBlank(cabCodes)){
            String [] cabCodeArray=cabCodes.split(",");
            params.put("cabCodes",cabCodeArray);
        }
        params.put("skuName",skuName);
        params.put("brandName",brandName);
        params.put("typeName",typeName);
        params.put("name",name);
        params.put("workOrderId",workOrderId);
        Page<CellSingleInventoryProductVO> list = cellSingleInventoryProductService.pageListSku4app(pg, params);
        if (list.getRecords().size()==0){
            params.put("workOrderId",null);
            list = cellSingleInventoryProductService.pageListSku4app(pg, params);
        }
        PageResult pageResult=new PageResult<>(list.getRecords(), list.getTotal());
//        List<SystemRuleInfo> systemRuleInfos=systemRuleInfoService.list();
        SystemRuleInfo systemRuleInfo=systemRuleInfoService.getRuleInfo();
        if (systemRuleInfo!=null){
            Boolean showPriceFlag=systemRuleInfo.getShowPriceFlag();
            pageResult.setTotalFlag(systemRuleInfo.getTotalFlag());
            if(systemRuleInfo.getTotalFlag()&&systemRuleInfo.getTotal()!=null&&systemRuleInfo.getTotal().doubleValue()>0){
                List<CellInventoryOutAmount> cellInventoryOutAmounts=cellInventoryOutAmountService.list(new QueryWrapper<CellInventoryOutAmount>().eq("creator", JwtUtils.getLoginUserId()));
                if (cellInventoryOutAmounts.size()>0){
                    pageResult.setCurrentTotal(cellInventoryOutAmounts.get(0).getTotal());
                }else{
                    pageResult.setCurrentTotal(BigDecimal.valueOf(0));
                }
                pageResult.setSettingTotal(systemRuleInfo.getTotal());
            }
            list.getRecords().forEach(item->{
                item.setShowPriceFlag(showPriceFlag);
            });
        }
        return pageResult;
    }

    @ApiOperation(value = "查询售刀柜产品库存", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "productCategoryId", value = "产品分类id", defaultValue = "", example = "", dataType = "Long", paramType = "query")
            ,@ApiImplicitParam(name = "productTypeId", value = "产品类型id", defaultValue = "", example = "", dataType = "Long", paramType = "query")
            ,@ApiImplicitParam(name = "skuName", value = "SKU名称", defaultValue = "", example = "", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "brandName", value = "品牌名称", defaultValue = "", example = "", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "typeName", value = "产品类型名称", defaultValue = "", example = "", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "cabCodes", value = "柜子编号", defaultValue = "", example = "", dataType = "String", paramType = "query")
    })
    @GetMapping("/listCellSingleInventoryProduct")
    public PageResult<CellSingleInventoryProductVO> listWx(
            @RequestParam(name = "page",  defaultValue = "1", required = false) Integer page
            ,@RequestParam(name = "limit",  defaultValue = "10", required = false) Integer limit
            ,@RequestParam(name = "sort",  defaultValue = "create_time", required = false) String sort
            ,@RequestParam(name = "order",  defaultValue = "desc", required = false) String order
            ,Long productCategoryId
            ,Long productTypeId
            ,String cabCodes
            ,String skuName
            ,String brandName
            ,String typeName
            ,String name
            ,String mainCabCode
    ) {
        Page<CellSingleInventoryProductVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }
        params.put("productCategoryId",productCategoryId);
        params.put("productTypeId",productTypeId);
        if (StringUtils.isNotBlank(cabCodes)){
            String [] cabCodeArray=cabCodes.split(",");
            params.put("cabCodes",cabCodeArray);
        }
        params.put("skuName",skuName);
        params.put("brandName",brandName);
        params.put("typeName",typeName);
        params.put("name",name);
        params.put("mainCabCode",mainCabCode);
        Page<CellSingleInventoryProductVO> list = cellSingleInventoryProductService.pageListSku4app(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @GetMapping("/detail/{id}")
    public JsonResult detail(@PathVariable("id") Long cellSingleInventoryProductId) {
        CellSingleInventoryProductVO cellSingleInventoryProductVO = cellSingleInventoryProductService.get4appById(cellSingleInventoryProductId);
        if (cellSingleInventoryProductVO == null) {
            return JsonResult.fail(localeMessage.getMessage("common.fail"));
        }
        return JsonResult.success(localeMessage.getMessage("common.success"), cellSingleInventoryProductVO);
    }

    @PostMapping()
    public JsonResult add(@Validated CellSingleInventoryProductForm cellSingleInventoryProductForm) {
        if (cellSingleInventoryProductService.add(cellSingleInventoryProductForm) > 0) {
            return JsonResult.success(localeMessage.getMessage("common.success"));
        }
        return JsonResult.fail(localeMessage.getMessage("common.fail"));
    }

    @ApiOperation(value = "获取柜子产品库存", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cabCodes", value = "柜子编码", defaultValue = "", example = "", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "skuCode", value = "SKu", defaultValue = "", example = "", dataType = "String", paramType = "query")
    })
    @GetMapping("/getCabSkuInventory")
    public JsonResult<CellSingleInventoryProductVO> getCabSkuInventory(String cabCodes,String skuCode){

        Map<String, Object> params = new HashMap<>();
        if (StringUtils.isNotBlank(cabCodes)){
            String [] cabCodeArray=cabCodes.split(",");
            params.put("cabCodes",cabCodeArray);
        }
        params.put("skuCode",skuCode);
        List<CellSingleInventoryProductVO> list = cellSingleInventoryProductService.getCabSkuInventory4app( params);
        if (list.size()>0){
            return JsonResult.success(list.get(0));
        }else{
            CellSingleInventoryProductVO cellSingleInventoryProductVO=new CellSingleInventoryProductVO();
            cellSingleInventoryProductVO.setQuantity(0);
            cellSingleInventoryProductVO.setCabCode(cabCodes);
            cellSingleInventoryProductVO.setSkuCode(skuCode);
            return JsonResult.success(cellSingleInventoryProductVO);
        }
//        return JsonResult.fail("未找到产品的库存数据");
    }

    @ApiOperation(value = "查询主柜物料", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
    })
    @GetMapping("/list4MainCab")
    public PageResult<CellSingleInventoryProductVO> list4MainCab(
            @RequestParam(defaultValue = "1") Integer page
            , @RequestParam(defaultValue = "10") Integer limit
            , @RequestParam(defaultValue = "create_time") String sort
            , @RequestParam(defaultValue = "asc") String order
            , String cabCode
            , String skuCode
            , String skuName
            , String mainCabCode
            , String queryFlag
            , Cabinet.CabinetNature cabinetNature
            , String skuSpecOld
    ){

        Page<CellInventorySpace> pg = new Page<>(page, limit);
        Map queryParam=new HashMap();
        queryParam.put("sort",StrUtil.toUnderlineCase(sort));
        queryParam.put("order",order);
        queryParam.put("cabCode",cabCode);
        queryParam.put("skuCode",skuCode);
        queryParam.put("skuName",skuName);
        queryParam.put("mainCabCode",mainCabCode);
        queryParam.put("queryFlag",queryFlag);
        queryParam.put("skuSpecOld",skuSpecOld);
        if (cabinetNature!=null){
            queryParam.put("cabinetNature",cabinetNature.getValue());
        }
        //搜索
        IPage<CellSingleInventoryProductVO> list = cellSingleInventoryProductService.pageList4MainCab(pg,queryParam);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "查询子柜物料", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
    })
    @GetMapping("/list4ChildCab")
    public PageResult<CellSingleInventoryProductVO> list4ChildCab(
            @RequestParam(defaultValue = "1") Integer page
            , @RequestParam(defaultValue = "10") Integer limit
            , @RequestParam(defaultValue = "create_time") String sort
            , @RequestParam(defaultValue = "asc") String order
            , String cabCode
            , String skuCode
            , String mainCabCode
            , String queryFlag
            , Cabinet.CabinetNature cabinetNature
    ){

        Page<CellInventorySpace> pg = new Page<>(page, limit);
        Map queryParam=new HashMap();
        queryParam.put("sort", StrUtil.toUnderlineCase(sort));
        queryParam.put("order",order);
        queryParam.put("cabCode",cabCode);
        queryParam.put("skuCode",skuCode);
        queryParam.put("mainCabCode",mainCabCode);
        queryParam.put("queryFlag",queryFlag);
        if (cabinetNature!=null){
            queryParam.put("cabinetNature",cabinetNature.getValue());
        }
        //搜索
        IPage<CellSingleInventoryProductVO> list = cellSingleInventoryProductService.pageList4ChildCab(pg,queryParam);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

}
